package com.bs.manager05.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.manager05.entity.User;
import com.bs.manager05.mapper.UserMapper;
import com.bs.manager05.utils.exception.NoUserException;
import com.bs.manager05.utils.security.LoginUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 小姚
 * @pakeageName:com.bs.manager05.service.impl
 * @ClassName:UserDetailsServiceImpl
 * @Descirption:
 * @data 2022/12/21 14:14
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserMapper mapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> lq=new LambdaQueryWrapper<>();
        lq.eq(User::getUsername,username);
        User user = mapper.selectOne(lq);
        //如果查询数据为null，则提示用户异常信息
        if (Objects.isNull(user)){
            throw new NoUserException("用户名或密码错误");
        }
        // 根据用户查询权限信息，添加LoginUser中
        //封装成userDetails对象并返回
        //进行加密！
        System.out.println("我是密码："+user.getPassword());

        return new LoginUser(user);
    }
}
